package com.shutuo.callnumqixintian.screeninfo;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class ScreenInfoParser {

    private static JSONObject getSingleData(JSONObject json) {
        if (json == null) return null;
        JSONArray dataArray = json.optJSONArray("data");
        if (dataArray == null || dataArray.length() == 0) return null;
        return dataArray.optJSONObject(0);
    }

    public static QXTianInfo parseResult(JSONObject jsonObject) throws Throwable {
        JSONObject json = getSingleData(jsonObject);
        if (json == null) return null;

        JSONObject owner_shop = json.optJSONObject("owner_shop");
        QXTianInfo qxTianInfo = null;

        if(owner_shop != null){
            JSONObject qxt_config_dict  = owner_shop.optJSONObject("qxt_config_dict");
            if(qxt_config_dict!=null){
                String qxt_token = qxt_config_dict.optString("qxt_token");
                if(!TextUtils.isEmpty(qxt_token)){
                    String qxt_sn = qxt_config_dict.optString("qxt_sn");
                    if(!TextUtils.isEmpty(qxt_sn)){
                        qxTianInfo = new QXTianInfo(qxt_token,qxt_sn);
                    }
                }
            }
        }

        if(qxTianInfo == null){
            qxTianInfo = new QXTianInfo("","");
        }
        return qxTianInfo;
    }

}
